//
//  CHWXJLViewController.m
//  CarFile
//
//  Created by 陈浩 on 2017/8/5.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHWXJLViewController.h"
#import "CHWXJLDetailsViewController.h"
#import "JL_Model.h"
#import "CHWXJLCell.h"
#import "CHTextField.h"
@interface CHWXJLViewController ()<UITableViewDelegate ,UITableViewDataSource>
@property (nonatomic ,strong) CHTextField *searchField;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,assign) int index;
@end

@implementation CHWXJLViewController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"维修记录";
    self.view.backgroundColor = [UIColor whiteColor];
    
    //  设置查询搜索框
    [self setupSearchView];
    
    [self setupTableView];
    
}

-(void)setupTableView{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,  CGRectGetMaxY(self.searchField.frame), UIScreenW, UIScreenH -CGRectGetMaxY(self.searchField.frame) -64)];
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        
        [self getWXJLData];
    }];
    tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        [self loadMoreWXJLData];
    }];
    tableView.mj_footer.automaticallyHidden = YES;
    [tableView.mj_header beginRefreshing];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    self.tableView = tableView;
}

-(void)setupSearchView{
    
    CHTextField *searchField = [[CHTextField alloc] initWithFrame:CGRectMake(15, 10, UIScreenW - 15 - 60, 34)];
    searchField.layer.cornerRadius = 5;
    searchField.layer.masksToBounds = YES;
    searchField.layer.borderWidth = 1;
    searchField.layer.borderColor = [UIColor colorWithHexString:@"d1d1d1" alpha:1.0f].CGColor;
    searchField.textField.placeholder = @"请输入车牌号搜索";
    searchField.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:searchField];
    self.searchField = searchField;
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(searchField.frame), 10, 60, 34)];
    btn.titleLabel.font = [UIFont systemFontOfSize:15];
    [btn setTitle:@"搜索" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithHexString:@"999999" alpha:1.0f] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(searchResult) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

-(void)searchResult{

    [MBProgressHUD showMessage:@"数据加载中..."];
    [self getWXJLData];
}



-(void)getWXJLData{
    _index = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    
    if (self.searchField.text.length >= 17) {
        params[@"vin"] = self.searchField.text;
    }else{
        params[@"carno"] = self.searchField.text;
    }
    params[@"companyid"] = self.companyid;
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"start"] = NORMOAL_DATE;
    params[@"end"] = [Util getLastDayTime];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_index];
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovrecordlist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [MBProgressHUD hideHUD];
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"repairlist"]) {
                JL_Model *model = [JL_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
            
        }
        self.tableView.placeholderImage = @"data_null";
        self.tableView.placeholder = @"非常抱歉，没有找到相关维修记录";
        [self.tableView reloadData];
 
    }];
    
}


-(void)loadMoreWXJLData{
    
    _index++;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (self.searchField.text.length >= 17) {
        params[@"vin"] = self.searchField.text;
    }else{
        params[@"carno"] = self.searchField.text;
    }
    params[@"areaid"] = [Util getValue:@"areaid"];
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"companyid"] = self.companyid;
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:(NSString *)[Util getValue:@"areaid"] encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"start"] = NORMOAL_DATE;
    params[@"end"] = [Util getLastDayTime];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_index];
    
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovrecordlist" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [self.tableView.mj_footer endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"repairlist"]) {
                JL_Model *model = [JL_Model objectWithKeyValues:dict];
                [self.datas addObject:model];
            }
            [self.tableView reloadData];
        }
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHWXJLCell *cell = [CHWXJLCell cellForTableView:tableView];
    cell.model = self.datas[indexPath.row];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
//    CHWXJLDetailsViewController *vc = [[CHWXJLDetailsViewController alloc] init];
//    vc.model = self.datas[indexPath.row];
//    [self.navigationController pushViewController:vc animated:YES];
    
    
    id obj = [LongriseRouter openURL:@"CHWXJLDetailsViewController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            
            params[@"model"] = self.datas[indexPath.row];
            
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:params block:^(id obj) {
                
                
            }];
        }
    }
    
    
    
}

#pragma mark LongriseRouterDelegater代理回调方法
-(void)setParams:(id)obj block:(LongriseRouterBlock)block
{
    
    if (obj && [obj isKindOfClass:[NSMutableDictionary class]]) {
        
        NSMutableDictionary *dict = obj;
        self.companyid = dict[@"companyid"];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
